<?php

namespace App\Http\Controllers\Admin\Users;

use App\Services\Admin\Users\GroupService;
use App\Http\Controllers\Controller;

class GroupController extends Controller
{
    public function groupList (){
        try {
            $size = request()->get('size',10);
            $page = request()->get('page',1);
            $offset = ($page - 1) * $size;
            $res = GroupService::groupList($size,$offset);
            return self::success(200,'查询完成',$res);
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function groupAdd (){
        try {
            $name = request()->post('name');
            if (empty($name)){
                throw new \Exception('参数不能为空');
            }
            $res = GroupService::groupAdd($name);
            return self::success(200,'添加成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function groupUpdate (){
        try {
            $data = request()->all();
            $res = GroupService::groupUpdate($data);
            return self::success(200,'修改成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function groupDelete (){
        try {
            $id = request()->get('id');
            $res = GroupService::groupDelete($id);
            return self::success(200,'删除成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function sendGroup (){
        try {
            $userid = request()->post('userid');
            $group = request()->post('group');
            $res = GroupService::sendGroup($userid,$group);
            return self::success(200,'分组设置成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }
}
